/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package studio.raptor.ddal.jdbc;


import com.google.common.base.Preconditions;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Collection;
import java.util.Collections;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import studio.raptor.ddal.core.engine.ProcessEngine;
import studio.raptor.ddal.core.engine.StatementContext;
import studio.raptor.ddal.core.executor.resultset.ResultData;
import studio.raptor.ddal.jdbc.adapter.AbstractStatementAdapter;
import studio.raptor.ddal.jdbc.processor.StatementProcessor;

/**
 * 支持分片的静态语句对象.
 *
 * @author Jack, Sam
 */
public class RaptorStatement extends AbstractStatementAdapter {

  private static Logger log = LoggerFactory.getLogger(RaptorStatement.class);

  private final RaptorConnection raptorConnection;

  private final int resultSetType;

  private final int resultSetConcurrency;

  private final int resultSetHoldability;

  private ResultSet currentResultSet;

  RaptorStatement(final RaptorConnection raptorConnection) {
    this(raptorConnection, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY,
        ResultSet.HOLD_CURSORS_OVER_COMMIT);
  }

  public RaptorStatement(final RaptorConnection raptorConnection, final int resultSetType,
      final int resultSetConcurrency, final int resultSetHoldability) {
    super(Statement.class);
    this.raptorConnection = raptorConnection;
    this.resultSetType = resultSetType;
    this.resultSetConcurrency = resultSetConcurrency;
    this.resultSetHoldability = resultSetHoldability;

    StatementContext statementContext = new StatementContext();
    statementContext.setPreparedStatement(false);
    this.raptorConnection.getProcessContext().setStatementContext(statementContext);
  }

  protected RaptorConnection getRaptorConnection() {
    return raptorConnection;
  }

  @Override
  public int getResultSetType() {
    return resultSetType;
  }

  @Override
  public void addBatch(String sql) throws SQLException {
    StatementContext statementContext = getRaptorConnection().getProcessContext().getStatementContext();
    statementContext.setAddBatch( true );
    statementContext.setExecuteBatch( false );
    getStatementProcessor().addBatch(sql);
  }

  @Override
  public void clearBatch() throws SQLException {
    setCurrentResultSet(null);
  }

  /**
   * 最终结果集，根据分片执行结果合并得到，与addBatch的提交顺序基本对不上
   * @return
   * @throws SQLException
   */
  @Override
  public int[] executeBatch() throws SQLException {
    StatementContext statementContext = getRaptorConnection().getProcessContext().getStatementContext();
    statementContext.setAddBatch( false );
    statementContext.setExecuteBatch( true );
    try{
      return getStatementProcessor().executeBatch();
    }finally {
      clearBatch();
    }
  }

  @Override
  public int getResultSetConcurrency() {
    return resultSetConcurrency;
  }

  @Override
  public int getResultSetHoldability() {
    return resultSetHoldability;
  }

  protected void setCurrentResultSet(ResultSet currentResultSet) {
    this.currentResultSet = currentResultSet;
  }

  @Override
  public Connection getConnection() throws SQLException {
    return raptorConnection;
  }

  @Override
  public ResultSet executeQuery(final String sql) throws SQLException {
    return getStatementProcessor().executeQuery(sql);
  }

  @Override
  public int executeUpdate(final String sql) throws SQLException {
    return getStatementProcessor().executeUpdate(sql);
  }

  @Override
  public int executeUpdate(final String sql, final int autoGeneratedKeys) throws SQLException {
    return getStatementProcessor().executeUpdate(sql, autoGeneratedKeys);
  }

  @Override
  public int executeUpdate(final String sql, final int[] columnIndexes) throws SQLException {
    return getStatementProcessor().executeUpdate(sql, columnIndexes);
  }

  @Override
  public int executeUpdate(final String sql, final String[] columnNames) throws SQLException {
    return getStatementProcessor().executeUpdate(sql, columnNames);
  }

  @Override
  public boolean execute(final String sql) throws SQLException {
    return getStatementProcessor().execute(sql);
  }

  @Override
  public boolean execute(final String sql, final int autoGeneratedKeys) throws SQLException {
    return getStatementProcessor().execute(sql, autoGeneratedKeys);
  }

  @Override
  public boolean execute(final String sql, final int[] columnIndexes) throws SQLException {
    return getStatementProcessor().execute(sql, columnIndexes);
  }

  @Override
  public boolean execute(final String sql, final String[] columnNames) throws SQLException {
    return getStatementProcessor().execute(sql, columnNames);
  }

  @Override
  public ResultSet getResultSet() throws SQLException {
    if (null == currentResultSet) {
      currentResultSet = new RaptorResultSet(
          getProcessEngine().getProcessContext().getStatementContext().getMergedResult());
    }
    return currentResultSet;
  }

  /**
   * PreparedStatement执行更新操作返回的记录数。为了兼容分两步执行更新操作
   * 的情况：限制性返回布尔值的execute方法，然后从Statement对象中获取更新条数。
   *
   * @return SQL语句更新条数
   * @throws SQLException sql exception
   */
  @Override
  public int getUpdateCount() throws SQLException {
    ResultData resultData;
    Preconditions.checkState(null != getRaptorConnection());
    Preconditions.checkState(null != getRaptorConnection().getProcessContext());
    Preconditions.checkState(
        null != (resultData = getRaptorConnection().getProcessContext().getStatementContext().getMergedResult()));
    return resultData.getAffectedRows();
  }

  @Override
  public Collection<? extends Statement> getRoutedStatements() throws SQLException {
    return Collections.emptyList();
  }

  private ProcessEngine getProcessEngine() {
    return this.raptorConnection.getProcessEngine();
  }

  private StatementProcessor getStatementProcessor() {
    return this.raptorConnection.getStatementProcessor();
  }

  @Override
  public void close() throws SQLException {
    this.raptorConnection.getProcessContext().clear();
    super.close();
  }
}
